{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "curr_path = str(Path().absolute()) \n",
    "parent_path = str(Path().absolute().parent) \n",
    "sys.path.append(parent_path) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/10 Loss: 0.008699130266904831\n",
      "2/10 Loss: 0.004475241992622614\n",
      "3/10 Loss: 0.003495733719319105\n",
      "4/10 Loss: 0.0028438889421522617\n",
      "5/10 Loss: 0.0023185270838439465\n",
      "6/10 Loss: 0.001978325191885233\n",
      "7/10 Loss: 0.0017454109620302916\n",
      "8/10 Loss: 0.001538722775876522\n",
      "9/10 Loss: 0.0013974674511700869\n",
      "10/10 Loss: 0.0012805062578991055\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "from torch.utils.data import DataLoader\n",
    "from torchvision import datasets\n",
    "import torchvision.transforms as transforms\n",
    "import torch.optim as optim\n",
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.conv1 = nn.Conv2d(1, 32, 3, 1)\n",
    "        self.conv2 = nn.Conv2d(32, 64, 3, 1)\n",
    "        self.dropout1 = nn.Dropout(0.25)\n",
    "        self.dropout2 = nn.Dropout(0.5)\n",
    "        self.fc1 = nn.Linear(9216, 128)\n",
    "        self.fc2 = nn.Linear(128, 10)\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.conv1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.conv2(x)\n",
    "        x = F.relu(x)\n",
    "        x = F.max_pool2d(x, 2)\n",
    "        x = self.dropout1(x)\n",
    "        x = torch.flatten(x, 1)\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.dropout2(x)\n",
    "        x = self.fc2(x)\n",
    "        output = F.log_softmax(x, dim=1)\n",
    "        return output\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "batch_size = 64\n",
    "n_epochs = 10\n",
    "train_loader = DataLoader(datasets.MNIST(parent_path+'/datasets/', train=True, download=False, transform=transforms.Compose([\n",
    "        transforms.ToTensor(),\n",
    "        transforms.Normalize((0.1307,), (0.3081,))\n",
    "        ])), \n",
    "                          batch_size=batch_size, \n",
    "                          shuffle=True)\n",
    "model = Net().to(device)\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.01)\n",
    "log_loss=[]\n",
    "for epoch in range(n_epochs):\n",
    "    total_loss = 0\n",
    "    for data, target in train_loader:\n",
    "        data,target = data.to(device),target.to(device)\n",
    "        model.zero_grad()\n",
    "        output = model(data)\n",
    "        loss = F.nll_loss(output, target)\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        total_loss+=loss\n",
    "        log_loss.append(loss)\n",
    "    total_loss /= len(train_loader.dataset)\n",
    "    print('{}/{} Loss:'.format(epoch+1, n_epochs), total_loss.item())"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "47dfafb046f9703cd15ca753999a7e7c95274099825c7bcc45b473d6496cd1b0"
  },
  "kernelspec": {
   "display_name": "Python 3.7.11 64-bit ('py37': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
